const express = require("express");
const router = express.Router();
const { conMysql } = require("../db");

//分页查询获取所有的企业信息
router.get("/getEnterprises", (req, res) => {
    //获取客户端传递过来的页码和每页显示的条数
    let { page, pageSize, enterprises = '' } = req.query;
    //判断页码和每页显示的条数是否为空
    if (!page) {
        page = 1;
    }
    if (!pageSize) {
        pageSize = 10;
    }
    //计算开始查询的索引
    let start = (page - 1) * pageSize;
    //查询所有的业务员
    const sql = `select * from db_enterprise  WHERE  (enterprise_name LIKE '%${enterprises}%' OR '%${enterprises}%' IS NULL) limit ${start},${pageSize};`;
    conMysql(sql)
        .then((result) => {
            //查询所有的业务员的总条数
            const sql = `select count(*) as count from db_enterprise WHERE  (enterprise_name LIKE '%${enterprises}%' OR '%${enterprises}%' IS NULL);`;
            conMysql(sql)
                .then((result2) => {
                    res.output("查询成功", 0, {
                        list: result,
                        count: result2[0].count,
                    });
                })
                .catch((err) => {
                    console.log(err);
                    res.output(err, 500, null);
                });
        })
        .catch((err) => {
            console.log(err);
            res.output(err, 500, null);
        });


})

module.exports = router;